<?php
/**
 * (model) CN-海外仓-箱唛生成工具（国内发海外仓）
 */
namespace App\Models\OverSeasIn;

use Illuminate\Database\Eloquent\Model;
class MarksPDF extends Model
{
    protected $table = 'os_in_marks_pdf';

    protected $guarded = [];

    public static $_template = [
        1 => '德国海外仓留仓',
        2 => '互邮英国海外仓',
    ];
    const FILE_NAME_DIR = 'marks';

    const FILE_DIR = 'app'.DIRECTORY_SEPARATOR.self::FILE_NAME_DIR;

    public static function storePath($deliveryNo,$template){
        $filename = $deliveryNo.'-MarksFile.pdf';
        return storage_path(self::FILE_DIR).DIRECTORY_SEPARATOR.$template.DIRECTORY_SEPARATOR.$filename;
    }

    public static function storeFile($deliveryNo,$template){
        if(self::isExist($deliveryNo,$template) == 0){
            return self::create([
                'delivery_no' => $deliveryNo,
                'template' => $template,
            ]);
        }
        return true;
    }

    public static function isExist($deliveryNo,$template){
        return self::query()
            ->where('delivery_no',$deliveryNo)
            ->where('template',$template)
            ->count();
    }

    protected static function boot()
    {
        parent::boot();

        static::deleting(function ($model){
            $file = self::storePath($model->delivery_no,$model->template);
            if(file_exists($file)){
                unlink($file);
            }
        });
    }
}
